import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;



public class ViewerMapaMain{
	
	Tiles t;
	Maps m;
	
	JPanel painelMapaMain;
	JPanel painelTile;
	JPanel painelMap = new JPanel();
	JPanel painelConfig = new JPanel();
	
	JButton buildMap = new JButton("Build Map");
	JButton buildArea = new JButton("Build Area");
	JButton getMap = new JButton("Get Map");
	JButton deselect = new JButton("Cancel Tile");
	
	JTextField col = new JTextField();
	JTextField lin = new JTextField();
	
	JScrollPane jspTile;
	JScrollPane jspMap;
	
	String path;
	
	JToggleButton btnmap[][];
	
	int firstTime = 0;
	
	protected ViewerMapaMain vmp = this;
	ViewerOverMap vom;
	
	
	public ViewerMapaMain(JPanel painel, ViewerOverMap vom) throws IOException {
			painelTile = new JPanel();
			this.vom = vom;
			painelMapaMain = painel;
			painelMapaMain.setLayout( new BorderLayout() );
			painelTile.setBorder(new TitledBorder ( new EtchedBorder (), "Tile Area" ) );
			painelMap.setBorder(new TitledBorder ( new EtchedBorder (), "Map Area" ) );
			painelConfig.setBorder(new TitledBorder ( new EtchedBorder (), "Config Area" ) );
			
			createPainelTile();
		    createPainelConfig();
			
			
			
			//painelEfeito.setBorder(new TitledBorder ( new EtchedBorder (), "Effect Area" ) );
			
			painelMapaMain.add(jspTile, BorderLayout.NORTH);
			//painelMapaMain.add(painelMap, BorderLayout.CENTER);
			painelMapaMain.add(painelConfig, BorderLayout.SOUTH);
			painelMapaMain.add(jspMap,BorderLayout.CENTER);
			//painelMapaMain.add(painelEfeito, BorderLayout.SOUTH);
		
}
	
	private void createPainelTile(){
		//painelTile.setPreferredSize(new Dimension(100,150));
		
	    jspTile = new JScrollPane(painelTile);
	    painelTile.add(getMap);
	    
	    getMap.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {	
				path = Utils.getMappath();
				
				if(path!=null){
					try {
						t = new Tiles(vmp) ;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//ImageIcon ii = new ImageIcon(path);
			    	//JButton btn = new JButton(ii);
			    	//btn.setBorder(null);
				    //painelTile.add(btn);
					painelTile.validate();
				}
				
				
//				mapTile = new BufferedImage(ii.getIconWidth(), ii.getIconHeight(), BufferedImage.TYPE_INT_RGB);
//				Graphics g = mapTile.createGraphics();
//				ii.paintIcon(null, g, 0,0);
//				g.dispose();
				
				//BufferedImage teste[][] = Utils.splitImage(mapTile, 1, 5);
			}
		});
	    jspTile.setPreferredSize(new Dimension(100,150) );
	}

	

	private void createPainelConfig() {
		// TODO Auto-generated method stub
		
		jspMap = new JScrollPane(painelMap);
		painelConfig.setLayout( new FlowLayout() );
		lin.setColumns(10);
		col.setColumns(10);
		
		buildArea.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int c;
				int l;
				
				if( col.getText().equals("") || lin.getText().equals("") ){
					 c = 0;
					 l = 0;
				}else{
					 c = Integer.parseInt(col.getText());
					 l = Integer.parseInt(lin.getText());
				}
				m = new Maps(vmp, vom, c,l);
				//System.out.println(vom);
				firstTime=+1;
			}
		});
		
		buildMap.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					final JFileChooser exportFileChooser = new JFileChooser();
					exportFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
					exportFileChooser.setApproveButtonText("Export");
					 int returnVal = exportFileChooser.showSaveDialog(buildMap.getParent());
					 if (returnVal == JFileChooser.APPROVE_OPTION) {
				            File outputFile = exportFileChooser.getSelectedFile();
				            if (outputFileIsValid(outputFile)) {
				            	if(m!=null && Utils.getChoose() != null){
				            	new Impressao(vmp,vom, outputFile);
				            	}else{
				            		JOptionPane.showConfirmDialog(null,"Erro, Sem Mapa Instaciado");
				            	}
				            }
				        }
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

			private boolean outputFileIsValid(File outputFile) {
				boolean fileIsValid = false;
		        if (outputFile.exists()) {
		            int result = JOptionPane.showConfirmDialog(
		                    buildMap.getParent(),
		                    "File exists, overwrite?", "File exists",
		                    JOptionPane.YES_NO_CANCEL_OPTION);
		            switch (result) {
		            case JOptionPane.YES_OPTION:
		                fileIsValid = true;
		                break;
		            default:
		                fileIsValid = false;
		            }
		        } else {
		            fileIsValid = true;
		        }
		        return fileIsValid;
		    }
		});
		
		deselect.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(vmp.t != null){
					vmp.t.getBtn().setSelected(false);
				}
				
			}
		});
		
		painelConfig.add(lin);
		painelConfig.add(col);
		painelConfig.add(buildArea);
		painelConfig.add(buildMap);
		painelConfig.add(deselect);
		painelConfig.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		
	}
	
	
public String getPath() {
	return path;
}

}
